/**
 * JAVACC DEMO 1.0
 * @copy right zbxsoft company All rights reserved. 
 * @Package com.dwusoft.buss.common  
 */
package com.dwusoft.ius;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.apache.api.manager.ProxyManager;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.database.constant.SpringContextLoader;
import com.apache.database.db.IDao;
import com.apache.tools.DataMap;
import com.dwusoft.ius.plugin.CustomMethodPlugin;
import com.dwusoft.ius.plugin.IusSqlPorxyPlugin;

/**
 * description:  
 * @author cbqiu 创建时间：2016-10-11  
 * @Copyright(c)2014:东吴软件技术有限公司
 */
public class IusSqlPorxyPluginImpl implements IusSqlPorxyPlugin {

	private IDao trpFeeDao;
	private Map<String, String> plugins = new HashMap<String, String>();

	public Object doInvoke(ParamsVo vo) {
		String key = String.valueOf(vo.getParams("sqlKeyId"));
		String methodKey = vo.getMethodKey();
		vo.setMethodKey("");
		if (Validator.isNull(methodKey))
			return null;
		String str = plugins.get(key);
		Map resultmap = new HashMap();
		if (Validator.isNotNull(str)) {
			String strs[] = str.split(",");
			for (int i = 0; i < strs.length; i++) {
				CustomMethodPlugin plugin = CustomMethodFactory.getInstance().getPluginByKey(strs[i]);
				Map map = null;
				if (Validator.isNotNull(methodKey) && "beforMethodKey".equalsIgnoreCase(methodKey)) {
					map = (Map) plugin.beforeInvoke(vo.getParams());
				} else if (Validator.isNotNull(methodKey) && "afterMethodKey".equalsIgnoreCase(methodKey)) {
					map = (Map) plugin.afterInvoke(vo.getParams());
				} else if (Validator.isNotNull(methodKey) && "invokeMethodKey".equalsIgnoreCase(methodKey)) {
					map = (Map) plugin.doInvoke(vo.getParams());
				}
				if (!Validator.isEmpty(map))
					resultmap.putAll(map);
			}
		}
		return resultmap;
	}

	@SuppressWarnings("unchecked")
	public void setPlugins(Map<String, String> plugins) {
		ProxyManager netProxyManager = (ProxyManager) SpringContextLoader.getBean("netProxyManager");
		ParamsVo params = new ParamsVo();
		params.setParams("doCode", "dymicSql");
		params.setParams("sqlKeyId", "TrpRules");
		params.setParams("resultType", "list");
		params.setKey("publicForSql");
		ResultEntity dymicSqlParamResult = (ResultEntity) netProxyManager.doInvoke(params);
		List<DataMap> list = (List) dymicSqlParamResult.getEntity();
		if (!Validator.isEmpty(list)) {
			for (int i = 0; i < list.size(); i++) {
				DataMap mp = list.get(i);
				this.plugins.put(String.valueOf(mp.get("rulesCode")), String.valueOf(mp.get("rulesName")));
			}
		}
	}

	public void setPluginInfo(String key, String value) {
		if (Validator.isNull(value)) {
			plugins.remove(key);
		} else {
			plugins.put(key, value);
		}
	}
}
